package org.example.dingtalkintegrationservice.dingtalk.process;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.dingtalkintegrationservice.exception.WrongFormatExceptionGroovy;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author pc
 */
@Component
public class CreateProcessPrepare {
    public String convert(String input){
        ObjectMapper objectMapper = new ObjectMapper();
        List<Map<String, Object>> maps;
        try{
            maps = objectMapper.readValue(input, new TypeReference<List<Map<String, Object>>>() {});
        }
        catch (JsonProcessingException e){
            throw new WrongFormatExceptionGroovy(input, e.getCause(),"toListMap1");
        }
        for (Map<String, Object> map : maps ) {
//            System.out.println(map.toString());
            if ("TableField" .equals( map.get("type"))) {
                if ((!map.containsKey("children"))){
                    throw new WrongFormatExceptionGroovy(map.toString(),"getChildren");
                }
                String children = map.get("children").toString();
                System.out.println(children);
                if (!children.contains("label")||!children.contains("type")){
                    throw new WrongFormatExceptionGroovy(children,"replaceLabelAndTypeString");
                }
                String newChildren = children.replace("label", "\"label\"").replace("type", "\"type\"");
                List<Map<String,Object>> childrenMap;
                try{
                    childrenMap=objectMapper.readValue(newChildren, new TypeReference<List<Map<String, Object>>>() {});
                }
                catch (JsonProcessingException e){
                    throw new WrongFormatExceptionGroovy(newChildren, e.getCause(),"toListMap2");
                }
//                for (Map<String,Object> map1:childrenMap){
//                    map1.put("id",map1.get("label"));
//                }
                map.put("children", childrenMap);
            }
        }
        return JSON.toJSONString(maps);
    }
//static  void main(String[] args){
//    def input="[{\"label\":\"总金额\",\"type\":\"TextField\"},{\"label\":\"据点\",\"type\":\"TextField\"},{\"label\":\"税率\",\"type\":\"TextField\"},{\"label\":\"业务部门\",\"type\":\"TextField\"},{\"label\":\"发票类型\",\"type\":\"TextField\"},{\"label\":\"供应商\",\"type\":\"TextField\"},{\"label\":\"新增数据\",\"type\":\"TableField\",\"children\":\"[{label:\\\"试制单号\\\",type:\\\"TextField\\\"},{label:\\\"项目编号\\\",type:\\\"TextField\\\"},{label:\\\"项目名称\\\",type:\\\"TextField\\\"},{label:\\\"代号\\\",type:\\\"TextField\\\"},{label:\\\"名称\\\",type:\\\"TextField\\\"},{label:\\\"数量\\\",type:\\\"TextField\\\"},{label:\\\"金额\\\",type:\\\"TextField\\\"}]\"},{\"label\":\"费用科目\",\"type\":\"TextField\"},{\"label\":\"税额\",\"type\":\"TextField\"},{\"label\":\"不含税\",\"type\":\"TextField\"},{\"label\":\"号码位数\",\"type\":\"TextField\"},{\"label\":\"代码位数\",\"type\":\"TextField\"},{\"label\":\"发票代码\",\"type\":\"TextField\"},{\"label\":\"开票号码\",\"type\":\"TextField\"},{\"label\":\"发票日期\",\"type\":\"TextField\"}]";
//    print(convert(input))
//}
//  def exec(String input){
//        String output = convert(input)
//        return output
//}
}